﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplLogic.ApplClasses;

namespace TCPClient.UI.UserControls
{
    public delegate void EventHandler(object sender, EventArgs e);

    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class SingleWorkerInShiftUserControl : UserControl
    {
        private bool m_IsClicked = false;
        private bool m_IsInChangeMode = false;
        private ApplWorker m_Worker = null;
        private CheckBox m_EnableManagerRegistraionsRef;
        private CheckBox m_EnableManagerRegistraionsSwapRef;
        public event EventHandler WorkerLabelClicked = null;
        public event EventHandler WorkerClicked = null;

        public SingleWorkerInShiftUserControl(ApplWorker worker)
        {
            InitializeComponent();
            m_Worker = worker;
        }

        public bool IsClicked
        {
            set { m_IsClicked = value; }
        }

        public bool IsInChangeMode
        {
            set { m_IsInChangeMode = value; }
            get { return m_IsInChangeMode; }
        }

        public void SetCheckBoxToListenTo(CheckBox checkBox1, CheckBox checkBox2)
        {
            m_EnableManagerRegistraionsRef = checkBox1;
            m_EnableManagerRegistraionsRef.CheckStateChanged += changeTheChangeMode;
            m_EnableManagerRegistraionsSwapRef = checkBox2;
            m_EnableManagerRegistraionsSwapRef.CheckStateChanged += changeTheChangeMode;
        }

        private void changeTheChangeMode(object sender, EventArgs e)
        {
            m_IsInChangeMode = (sender as CheckBox).Checked;
        }

        private void onWorkerLabelClicked(object sender, EventArgs e)
        {
            if (WorkerLabelClicked != null)
            {
                WorkerLabelClicked.Invoke(sender, e);
            }
        }

        private void onWorkerClicked()
        {
            if (WorkerClicked != null)
            {
                WorkerClicked.Invoke(m_Worker,
                                          new SingleWorkerInShiftEventArgs()
                                          {
                                              StartTime = (Parent as OneShiftManagerUserControl).StartTime,
                                              EndTime = (Parent as OneShiftManagerUserControl).EndTime
                                          });
            }
        }

        public SingleWorkerInShiftUserControl()
        {
            InitializeComponent();
        }

        public ApplWorker Worker
        {
            get { return m_Worker; }
            set { m_Worker = value; }
        }

        public void SetTextInLabel(string text, Color color)
        {
            this.linkLabelSingleWorker.Text = text;
            linkLabelSingleWorker.BackColor = color;
        }

        public Label Label
        {
            get { return this.linkLabelSingleWorker; }
        }

        private void linkLabelSingleWorker_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            onWorkerLabelClicked(this, e);
        }

        private void SingleWorkerInShiftUserControl_Click(object sender, EventArgs e)
        {
            setIsInChangeMode();
            if (m_IsInChangeMode)
            {
                this.BackColor = Color.Peru;
                m_IsClicked = true;
                onWorkerClicked();
            }
        }

        private void setIsInChangeMode()
        {
            m_IsInChangeMode &= m_EnableManagerRegistraionsRef.Visible && m_EnableManagerRegistraionsSwapRef.Visible;
        }

        private void SingleWorkerInShiftUserControl_MouseEnter(object sender, EventArgs e)
        {
            setIsInChangeMode();
            if (m_IsInChangeMode)
            {
                this.BackColor = Color.Peru;
            }
        }

        private void SingleWorkerInShiftUserControl_MouseLeave(object sender, EventArgs e)
        {
            setIsInChangeMode();
            if (m_IsInChangeMode)
            {
                if (!m_IsClicked)
                {
                    BackColor = SystemColors.ActiveCaption;
                }
            }
        }



    }

    public class SingleWorkerInShiftEventArgs : EventArgs
    {
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
    }
}
